package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Adres;
import hibernate.model.Kullanici;
import hibernate.model.Temsilci;
import hibernate.service.EmmyService;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("TemsilciBean")
@Scope("session")
public class TemsilciBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Temsilci currentTemsilci; //online user	
	private Temsilci selectedTemsilci; //selectedUser
	private Temsilci newUser;
	private List<Temsilci> temsilciList;
	
	private String tcKontrolText;
	private String klcAdKontrolText;
	private String DgTarihBitis;
	
	@Override
	@PostConstruct
	public void init(){	
		
	}	
	
	@Override
	public String restart(){
		init();
		return "list";
	}

	@Override
	public String deleteView(){
		emmyService.delete(currentTemsilci.getKullaniciid());
		init();
		return "list";
	}

	@Override
	public String viewNew() {
		System.out.println("Temsilci Ekle");
		newUser = new Temsilci();
		newUser.setAdres(new Adres());
		newUser.setKullanici(new Kullanici());
		newUser.getKullanici().setGrup(2);
		newUser.getAdres().setGrupid(2);
		newUser.setSirketprofil(currentTemsilci.getSirketprofil());
		return "temsilciKayit";
	}

	@Override
	public String viewDetail() {/*
		List<Temsilci> valueList = (List<Temsilci>) temsilciTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = temsilciTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentTemsilci= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentTemsilci.getKullaniciid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		FacesMessage msg;
		if(emmyService.saveOrUpdate(currentTemsilci)){
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Process Completed...");  
	        FacesContext.getCurrentInstance().addMessage(null, msg);
			return "OK";
		}else{		
			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "unsuccessful", "İşlem Başarısız, Tekrar deneyiniz.");  
	        FacesContext.getCurrentInstance().addMessage(null, msg);
			return "ERR";
		}				
	}

	public void saveNewUser(ActionEvent actionEvent) throws MyException {  
		 FacesMessage msg;
		 System.out.println("Save new User");
	        if(emmyService.saveOrUpdate(newUser)){
	        	msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Process Completed...");  
		        FacesContext.getCurrentInstance().addMessage(null, msg);
	        }else{
	        	msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "unsuccessful", "İşlem Başarısız, Tekrar deneyiniz.");  
		        FacesContext.getCurrentInstance().addMessage(null, msg);
	        }
	}
	
	public void TcKontrol() {						
		try{
			Temsilci tem =  (Temsilci) emmyService.
					 getQuery("findTmslcTcKimlik", "tTcKimlik", newUser.getTcKimlik()).get(0); 
			if(tem != null){
				tcKontrolText = "TC kimlik daha önceden kayıtlı";
				newUser.setTcKimlik(0L);
			}				
		}catch(java.lang.IndexOutOfBoundsException ioe){
			System.out.println("Tc Kimlik kayıtlı değil");
			tcKontrolText = "OK";
		}
		catch(Exception e){
			tcKontrolText = e.toString();
		}
	}
	
	public void kullaniciAdKontrol(){
		try{			
			System.out.println("1");
			System.out.println("::: ="+newUser.getKullanici().getKullaniciAdi() );
			System.out.println("text : "+klcAdKontrolText);
			Kullanici klc = (Kullanici) emmyService.
					 getQuery("findKlcKulAdi", "kKulAdi", ""+newUser.getKullanici().getKullaniciAdi()).get(0);
			System.out.println("2");
			if(klc != null){
				klcAdKontrolText = "Kullanıcı Adı daha önceden kayıtlı";
				System.out.println("3");
				newUser.getKullanici().setKullaniciAdi("");
				System.out.println("4");
			}	
		}catch(java.lang.IndexOutOfBoundsException ioe){
			System.out.println("Kullanıcı Adı kayıtlı değil");
			klcAdKontrolText = "OK";
		}
		catch(Exception e){
			klcAdKontrolText = e.toString();
		}
	}
	@SuppressWarnings("unchecked")
	public String getDetayliTemsilci(){
		String query = createQuery();
		if(!query.equals("list")){
			temsilciList = (List<Temsilci>) emmyService.getQuery(createQuery());
		}		
		return "list";
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String createQuery() {
		Temsilci tmsl  = currentTemsilci;		
		String query = "from Temsilci";				
		
		if(tmsl == null){
			return query;
		}
		if(tmsl.getTcKimlik() != 0){
			temsilciList = (List<Temsilci>) emmyService
					.getQuery("findTmslcTcKimlik","tTcKimlik",tmsl.getTcKimlik());
			return "list";
		}
		if(tmsl.getSirketprofil().getFirmaid() != 0){
			temsilciList = (List<Temsilci>) emmyService
					.getQuery("findTmslcUretici","tUretici",tmsl.getTcKimlik());
			return "list";
		}
		if(tmsl.getAd() != null){
			if(query.indexOf("where") != -1){
				query += " and ad like '%"+tmsl.getAd()+"%'";
				
			}
			else{
				query += " where ad like '%"+tmsl.getAd()+"%'";			
			}
		}
		if(tmsl.getSoyad() != null){
			if(query.indexOf("where") != -1){
				query += " and soyad like '%"+tmsl.getSoyad()+"%'";
				
			}
			else{
				query += " where soyad like '%"+tmsl.getSoyad()+"%'";			
			}
		}
		if(tmsl.getDogumTarihi() != null){
			if(query.indexOf("where") != -1){
				query += " and (dogumTarihi between "+tmsl.getDogumTarihi()+" and "+DgTarihBitis+")";
				
			}
			else{
				query += " where (dogumTarihi between "+tmsl.getDogumTarihi()+" and "+DgTarihBitis+")";			
			}
		}
		if(tmsl.getTel1() != 0){
			if(query.indexOf("where") != -1){
				query += " and tel1 like '%"+tmsl.getTel1()+"%'";
				
			}
			else{
				query += " where tel1 like '%"+tmsl.getTel1()+"%'";			
			}
		}
		if(tmsl.getTel2() != 0){
			if(query.indexOf("where") != -1){
				query += " and tel2 like '%"+tmsl.getTel2()+"%'";
				
			}
			else{
				query += " where tel2 like '%"+tmsl.getTel2()+"%'";			
			}
		}
		if(tmsl.getFax() != 0){
			if(query.indexOf("where") != -1){
				query += " and fax="+tmsl.getFax()+"";
				
			}
			else{
				query += " where fax="+tmsl.getFax()+"";			
			}
		}
		if(tmsl.getEmail() != null){
			if(query.indexOf("where") != -1){
				query += " and email like '%"+tmsl.getEmail()+"%'";
				
			}
			else{
				query += " where email like '%"+tmsl.getEmail()+"%'";			
			}
		}
		if(tmsl.getYetkisi() != 0){
			if(query.indexOf("where") != -1){
				query += " and yetkisi="+tmsl.getYetkisi()+"";
				
			}
			else{
				query += " where yetkisi="+tmsl.getYetkisi()+"";			
			}
		}
		if(tmsl.getUnvan() != null){
			if(query.indexOf("where") != -1){
				query += " and unvan like '%"+tmsl.getUnvan()+"%'";
				
			}
			else{
				query += " where unvan like '%"+tmsl.getUnvan()+"%'";			
			}
		}
		if(tmsl.getGrup() != 0){
			if(query.indexOf("where") != -1){
				query += " and grup="+tmsl.getGrup()+"";
				
			}
			else{
				query += " where grup="+tmsl.getGrup()+"";			
			}
		}
		if(tmsl.getKisiselWebAdd() != null){
			if(query.indexOf("where") != -1){
				query += " and kisiselWebAdd like '%"+tmsl.getKisiselWebAdd()+"%'";
				
			}
			else{
				query += " where kisiselWebAdd like '%"+tmsl.getKisiselWebAdd()+"%'";			
			}
		}
		return query;
	}

	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	public Temsilci getCurrentTemsilci() {
		return currentTemsilci;
	}

	public void setCurrentTemsilci(Temsilci currentTemsilci) {
		this.currentTemsilci = currentTemsilci;
	}

	public Temsilci getSelectedTemsilci() {
		return selectedTemsilci;
	}

	public void setSelectedTemsilci(Temsilci selectedTemsilci) {
		this.selectedTemsilci = selectedTemsilci;
	}

	public List<Temsilci> getTemsilciList() {
		return temsilciList;
	}

	public void setTemsilciList(List<Temsilci> temsilciList) {
		this.temsilciList = temsilciList;
	}

	public String getDgTarihBitis() {
		return DgTarihBitis;
	}

	public void setDgTarihBitis(String dgTarihBitis) {
		DgTarihBitis = dgTarihBitis;
	}

	public Temsilci getNewUser() {
		return newUser;
	}

	public void setNewUser(Temsilci newUser) {
		this.newUser = newUser;
	}

	public String getTcKontrolText() {
		return tcKontrolText;
	}

	public void setTcKontrolText(String tcKontrolText) {
		this.tcKontrolText = tcKontrolText;
	}

	public String getKlcAdKontrolText() {
		return klcAdKontrolText;
	}

	public void setKlcAdKontrolText(String klcAdKontrolText) {
		this.klcAdKontrolText = klcAdKontrolText;
	}
}